package jnpf.service.impl;

import jnpf.entity.ZXGL_XiangMuRenWuEntity;
import jnpf.mapper.ZXGL_XiangMuRenWuMapper;
import jnpf.service.ZXGL_XiangMuRenWuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.util.RandomUtil;

import java.math.BigDecimal;


import jnpf.model.zxgl_xiangmurenwu.ZXGL_XiangMuRenWuPagination;
import cn.hutool.core.util.ObjectUtil;
import jnpf.permission.service.AuthorizeService;

import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.TableField;

import java.util.stream.Collectors;

import jnpf.entity.ZXGL_XiangMuRenWuMXEntity;
import jnpf.service.ZXGL_XiangMuRenWuMXService;


import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.SimpleDateFormat;

import jnpf.util.*;

import java.util.*;

/**
 * ZXGL_XiangMuRenWu
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-08
 */
@Service
public class ZXGL_XiangMuRenWuServiceImpl extends ServiceImpl<ZXGL_XiangMuRenWuMapper, ZXGL_XiangMuRenWuEntity> implements ZXGL_XiangMuRenWuService {


    @Autowired
    private UserProvider userProvider;

    @Autowired
    private AuthorizeService authorizeService;


    @Autowired
    private ZXGL_XiangMuRenWuMXService zXGL_XiangMuRenWuMXService;


    @Override
    public List<ZXGL_XiangMuRenWuEntity> getList(ZXGL_XiangMuRenWuPagination zXGL_XiangMuRenWuPagination) {

        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuRenWuEntity> zXGL_XiangMuRenWuQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ZXGL_XiangMuRenWuMXEntity> zXGL_XiangMuRenWuMXQueryWrapper = new QueryWrapper<>();
        int zXGL_XiangMuRenWuNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getSuoshuxiangmu())) {
            zXGL_XiangMuRenWuNum++;
            zXGL_XiangMuRenWuQueryWrapper.lambda().like(ZXGL_XiangMuRenWuEntity::getSuoshuxiangmu, zXGL_XiangMuRenWuPagination.getSuoshuxiangmu());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmuid())) {
            zXGL_XiangMuRenWuNum++;
            zXGL_XiangMuRenWuQueryWrapper.lambda().like(ZXGL_XiangMuRenWuEntity::getXiangmuid, zXGL_XiangMuRenWuPagination.getXiangmuid());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmukaishishijian())) {
            zXGL_XiangMuRenWuNum++;
            List<String> XiangmukaishishijianList = zXGL_XiangMuRenWuPagination.getXiangmukaishishijian();
            Long fir = Long.valueOf(XiangmukaishishijianList.get(0));
            Long sec = Long.valueOf(XiangmukaishishijianList.get(1));

            zXGL_XiangMuRenWuQueryWrapper.lambda().ge(ZXGL_XiangMuRenWuEntity::getXiangmukaishishijian, new Date(fir))
                    .le(ZXGL_XiangMuRenWuEntity::getXiangmukaishishijian, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmujieshushijian())) {
            zXGL_XiangMuRenWuNum++;
            List<String> XiangmujieshushijianList = zXGL_XiangMuRenWuPagination.getXiangmujieshushijian();
            Long fir = Long.valueOf(XiangmujieshushijianList.get(0));
            Long sec = Long.valueOf(XiangmujieshushijianList.get(1));

            zXGL_XiangMuRenWuQueryWrapper.lambda().ge(ZXGL_XiangMuRenWuEntity::getXiangmujieshushijian, new Date(fir))
                    .le(ZXGL_XiangMuRenWuEntity::getXiangmujieshushijian, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmuzerenren())) {
            zXGL_XiangMuRenWuNum++;
            zXGL_XiangMuRenWuQueryWrapper.lambda().eq(ZXGL_XiangMuRenWuEntity::getXiangmuzerenren, zXGL_XiangMuRenWuPagination.getXiangmuzerenren());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmuzerenbumen())) {
            zXGL_XiangMuRenWuNum++;
            zXGL_XiangMuRenWuQueryWrapper.lambda().eq(ZXGL_XiangMuRenWuEntity::getXiangmuzerenbumen, zXGL_XiangMuRenWuPagination.getXiangmuzerenbumen());
        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuRenWuQueryWrapper.lambda().in(ZXGL_XiangMuRenWuEntity::getId, AllIdList);
        }
        /**
         * 数据权限 此处手动写入菜单id
         */
//        String moduleId = "";
//        if (!userProvider.get().getIsAdministrator()) {
//            Object conditionObj = authorizeService.getCondition(zXGL_XiangMuRenWuQueryWrapper, userProvider.get(), moduleId);
//            if (ObjectUtil.isEmpty(conditionObj)) {
//                return new ArrayList<>();
//            }
//            zXGL_XiangMuRenWuQueryWrapper = (QueryWrapper<ZXGL_XiangMuRenWuEntity>) authorizeService.getCondition(zXGL_XiangMuRenWuQueryWrapper, userProvider.get(), moduleId);
//        }
        //排序
        if (StringUtil.isEmpty(zXGL_XiangMuRenWuPagination.getSidx())) {
            zXGL_XiangMuRenWuQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuRenWuEntity::getDw);
        } else {
            try {
                String sidx = zXGL_XiangMuRenWuPagination.getSidx();
                ZXGL_XiangMuRenWuEntity zXGL_XiangMuRenWuEntity = new ZXGL_XiangMuRenWuEntity();
                Field declaredField = zXGL_XiangMuRenWuEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_XiangMuRenWuQueryWrapper = "asc".equals(zXGL_XiangMuRenWuPagination.getSort().toLowerCase()) ? zXGL_XiangMuRenWuQueryWrapper.orderByAsc(value) : zXGL_XiangMuRenWuQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_XiangMuRenWuEntity> page = new Page<>(zXGL_XiangMuRenWuPagination.getCurrentPage(), zXGL_XiangMuRenWuPagination.getPageSize());
            IPage<ZXGL_XiangMuRenWuEntity> userIPage = this.page(page, zXGL_XiangMuRenWuQueryWrapper);
            return zXGL_XiangMuRenWuPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_XiangMuRenWuEntity> list = new ArrayList();
            return zXGL_XiangMuRenWuPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_XiangMuRenWuEntity> getTypeList(ZXGL_XiangMuRenWuPagination zXGL_XiangMuRenWuPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuRenWuEntity> zXGL_XiangMuRenWuQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ZXGL_XiangMuRenWuMXEntity> zXGL_XiangMuRenWuMXQueryWrapper = new QueryWrapper<>();
        int zXGL_XiangMuRenWuNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getSuoshuxiangmu())) {
            zXGL_XiangMuRenWuNum++;
            zXGL_XiangMuRenWuQueryWrapper.lambda().like(ZXGL_XiangMuRenWuEntity::getSuoshuxiangmu, zXGL_XiangMuRenWuPagination.getSuoshuxiangmu());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmuid())) {
            zXGL_XiangMuRenWuNum++;
            zXGL_XiangMuRenWuQueryWrapper.lambda().like(ZXGL_XiangMuRenWuEntity::getXiangmuid, zXGL_XiangMuRenWuPagination.getXiangmuid());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmukaishishijian())) {
            zXGL_XiangMuRenWuNum++;
            List<String> XiangmukaishishijianList = zXGL_XiangMuRenWuPagination.getXiangmukaishishijian();
            Long fir = Long.valueOf(XiangmukaishishijianList.get(0));
            Long sec = Long.valueOf(XiangmukaishishijianList.get(1));

            zXGL_XiangMuRenWuQueryWrapper.lambda().ge(ZXGL_XiangMuRenWuEntity::getXiangmukaishishijian, new Date(fir))
                    .le(ZXGL_XiangMuRenWuEntity::getXiangmukaishishijian, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmujieshushijian())) {
            zXGL_XiangMuRenWuNum++;
            List<String> XiangmujieshushijianList = zXGL_XiangMuRenWuPagination.getXiangmujieshushijian();
            Long fir = Long.valueOf(XiangmujieshushijianList.get(0));
            Long sec = Long.valueOf(XiangmujieshushijianList.get(1));

            zXGL_XiangMuRenWuQueryWrapper.lambda().ge(ZXGL_XiangMuRenWuEntity::getXiangmujieshushijian, new Date(fir))
                    .le(ZXGL_XiangMuRenWuEntity::getXiangmujieshushijian, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmuzerenren())) {
            zXGL_XiangMuRenWuNum++;
            zXGL_XiangMuRenWuQueryWrapper.lambda().eq(ZXGL_XiangMuRenWuEntity::getXiangmuzerenren, zXGL_XiangMuRenWuPagination.getXiangmuzerenren());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuRenWuPagination.getXiangmuzerenbumen())) {
            zXGL_XiangMuRenWuNum++;
            zXGL_XiangMuRenWuQueryWrapper.lambda().eq(ZXGL_XiangMuRenWuEntity::getXiangmuzerenbumen, zXGL_XiangMuRenWuPagination.getXiangmuzerenbumen());
        }

        /**
         * 数据权限 此处手动写入菜单id
         */
//        String moduleId = "";
//        if (!userProvider.get().getIsAdministrator()) {
//            Object conditionObj = authorizeService.getCondition(zXGL_XiangMuRenWuQueryWrapper, userProvider.get(), moduleId);
//            if (ObjectUtil.isEmpty(conditionObj)) {
//                return new ArrayList<>();
//            }
//            zXGL_XiangMuRenWuQueryWrapper = (QueryWrapper<ZXGL_XiangMuRenWuEntity>) authorizeService.getCondition(zXGL_XiangMuRenWuQueryWrapper, userProvider.get(), moduleId);
//        }
        if (AllIdList.size() > 0) {
            zXGL_XiangMuRenWuQueryWrapper.lambda().in(ZXGL_XiangMuRenWuEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_XiangMuRenWuPagination.getSidx())) {
            zXGL_XiangMuRenWuQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuRenWuEntity::getDw);
        } else {
            try {
                String sidx = zXGL_XiangMuRenWuPagination.getSidx();
                ZXGL_XiangMuRenWuEntity zXGL_XiangMuRenWuEntity = new ZXGL_XiangMuRenWuEntity();
                Field declaredField = zXGL_XiangMuRenWuEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_XiangMuRenWuQueryWrapper = "asc".equals(zXGL_XiangMuRenWuPagination.getSort().toLowerCase()) ? zXGL_XiangMuRenWuQueryWrapper.orderByAsc(value) : zXGL_XiangMuRenWuQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_XiangMuRenWuEntity> page = new Page<>(zXGL_XiangMuRenWuPagination.getCurrentPage(), zXGL_XiangMuRenWuPagination.getPageSize());
                IPage<ZXGL_XiangMuRenWuEntity> userIPage = this.page(page, zXGL_XiangMuRenWuQueryWrapper);
                return zXGL_XiangMuRenWuPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_XiangMuRenWuEntity> list = new ArrayList();
                return zXGL_XiangMuRenWuPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_XiangMuRenWuQueryWrapper);
        }
    }

    @Override
    public ZXGL_XiangMuRenWuEntity getInfo(String id) {
        QueryWrapper<ZXGL_XiangMuRenWuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuRenWuEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_XiangMuRenWuEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_XiangMuRenWuEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public void delete(ZXGL_XiangMuRenWuEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }

    //子表方法
    @Override
    public List<ZXGL_XiangMuRenWuMXEntity> GetZXGL_XiangMuRenWuMXList(String id) {
        QueryWrapper<ZXGL_XiangMuRenWuMXEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuRenWuMXEntity::getId, id);
        return zXGL_XiangMuRenWuMXService.list(queryWrapper);
    }

    //列表子表数据方法

}